﻿using System;
using System.Windows.Forms;

namespace DeVierKoeden.Dynamicweb.CreateSite
{
  /// <summary>
  /// Helper form to configure custom source settings.
  /// </summary>
  public partial class ConfigureCustomSettings : Form
  {
    /// <summary>
    /// Gets or sets a value that determines of the ZIP file contains a custom application.
    /// </summary>
    public bool ZipHasCustomApplication { get; set; }

    /// <summary>
    /// Gets or sets a value that determines of the ZIP file contains a custom Files folder.
    /// </summary>
    public bool ZipHasCustomFiles { get; set; }

    /// <summary>
    /// Gets or sets a value that determines of the ZIP file contains a custom database.
    /// </summary>
    public bool ZipHasCustomDatabase { get; set; }


    /// <summary>
    /// Initializes a new instance of the ConfigureCustomSettings class.
    /// </summary>
    public ConfigureCustomSettings()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Configures the dialog with the appropriate settings.
    /// </summary>
    public void Initialize()
    {
      if (!ZipHasCustomApplication)
      {
        appUseZip.Enabled = false;
        appUseStandard.Checked = true;
      }
      if (!ZipHasCustomDatabase && !ZipHasCustomFiles)
      {
        noFilesOrDatabase.Text = "Your ZIP has no custom database and no Files Folder. Specify what you would like to use for the database and Files folder.";
        databaseAndFilesUseZip.Visible = false;
        databaseAndFilesUseDefaultEmptyData.Top = 69;
      }
      else
      {
        if (!ZipHasCustomDatabase || !ZipHasCustomFiles)
        {
          noFilesOrDatabase.Text = string.Format("Your ZIP has no custom {0}. Specify what you would like to use for the {0}.", ZipHasCustomFiles? "database" : "Files folder.");
          databaseAndFilesUseZip.Visible = false;
          databaseAndFilesUseDefaultEmptyData.Top = 69;
        }
        else
        {
          noFilesOrDatabase.Visible = false;
          databaseAndFilesUseZip.Top = 23;
          databaseAndFilesUseDefaultEmptyData.Top = 69;
        }
      }
    }

    private void ConfigureCustomSettings_Load(object sender, EventArgs e)
    {
      Height = 210;
      SelectApplication.Top = 12;
      SelectDatabaseAndFilesFolder.Top = 12;
      SelectDatabaseAndFilesFolder.Visible = false;
    }

    private void ToggleShowApplication(bool mustShow)
    {
      SelectApplication.Visible = mustShow;
    }

    private void ToggleShowDatabaseAndFile(bool mustShow)
    {
      SelectDatabaseAndFilesFolder.Visible = mustShow;
    }

    private void Step1Goto2_Click(object sender, EventArgs e)
    {
      GotoStep2();
    }

    private void GotoStep1()
    {
      ToggleShowApplication(true);
      ToggleShowDatabaseAndFile(false);
    }

    private void GotoStep2()
    {
      ToggleShowApplication(false);
      ToggleShowDatabaseAndFile(true);
    }

    private void FinishConfiguration()
    {
      DialogResult = DialogResult.OK;
      Close();
    }

    /// <summary>
    /// Gets the user configured settings from the dialog.
    /// </summary>
    public CustomSourceSettings CustomSourceSettings
    {
      get
      {
        var customSourceSettings = new CustomSourceSettings();
        if (appUseZip.Checked) { customSourceSettings.ApplicationSource = CustomSetupApplicationType.UseFromZip; }
        if (appUseStandard.Checked) { customSourceSettings.ApplicationSource = CustomSetupApplicationType.UseStandardDynamicwebApp; }
        if (appUseEmptyCustom.Checked) { customSourceSettings.ApplicationSource = CustomSetupApplicationType.CreateEmptyCustom; }

        if (ZipHasCustomDatabase && ZipHasCustomFiles)
        {
          if (databaseAndFilesUseZip.Checked)
          {
            customSourceSettings.DatabaseSource = CustomSetupSourceType.UseFromZip;
            customSourceSettings.FilesSource = CustomSetupSourceType.UseFromZip;
          }
          if (databaseAndFilesUseDefaultEmptyData.Checked)
          {
            customSourceSettings.DatabaseSource = CustomSetupSourceType.UseFromDefaultEmptyData;
            customSourceSettings.FilesSource = CustomSetupSourceType.UseFromDefaultEmptyData;
          }
        }
        else
        {
          if (ZipHasCustomFiles)
          {
            customSourceSettings.FilesSource = CustomSetupSourceType.UseFromZip;

            if (databaseAndFilesUseZip.Checked)
            {
              customSourceSettings.DatabaseSource = CustomSetupSourceType.UseFromZip;
            }
            if (databaseAndFilesUseDefaultEmptyData.Checked)
            {
              customSourceSettings.DatabaseSource = CustomSetupSourceType.UseFromDefaultEmptyData;
            }
          }
          else
          {
            customSourceSettings.DatabaseSource = CustomSetupSourceType.UseFromZip;

            if (databaseAndFilesUseZip.Checked)
            {
              customSourceSettings.FilesSource = CustomSetupSourceType.UseFromZip;
            }
            if (databaseAndFilesUseDefaultEmptyData.Checked)
            {
              customSourceSettings.FilesSource = CustomSetupSourceType.UseFromDefaultEmptyData;
            }
          }
        }

        return customSourceSettings;
      }
    }

    private void stepBothGoto2_Click(object sender, EventArgs e)
    {
      GotoStep1();
    }

    private void FinishFilesAndDatabase_Click(object sender, EventArgs e)
    {
      FinishConfiguration();
    }
  }
}
